package cn.tmall.controller;

import cn.tmall.pojo.Category;
import cn.tmall.service.CategoryService;
import cn.tmall.util.ImageUtil;
import cn.tmall.util.Page;
import cn.tmall.util.UploadedImageFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("")
public class CategoryController {

    @Resource(name = "CategoryServiceImpl")
    private CategoryService categoryService;

    @RequestMapping("admin_category_list")
    public String list(Model model, Page page){
        //这个page参数自动接受换页时传过来的start参数的parameter修改
        PageHelper.offsetPage(page.getStart(),page.getCount()); //只要这一句话，后面的list查询自动加上limit，我也不知道为什么
        List<Category> categoryList= categoryService.list();
        int total = (int) new PageInfo<>(categoryList).getTotal();
        page.setTotal(total);

        model.addAttribute("page", page);
        model.addAttribute("categoryList", categoryList);
        return "admin/listCategory";
    }

    @RequestMapping("admin_category_add")
    public String add(Model model, HttpSession session, Category category, UploadedImageFile uploadedImageFile) throws IOException {
        categoryService.add(category);

        File imageFolder = new File(session.getServletContext().getRealPath("img/category"));
        File file = new File(imageFolder, category.getId() + ".jpg");   //第一个参数是目录，第二个参数是文件名
        //不存在文件夹就创建
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        uploadedImageFile.getImage().transferTo(file);
        BufferedImage bufferedImage = ImageUtil.change2jpg(file);
        ImageIO.write(bufferedImage, "jpg", file);
        return "redirect:/admin_category_list";
    }

    @RequestMapping("admin_category_delete")
    public String delete(int id, HttpSession session){
        categoryService.delete(id);
        File imageFolder = new File(session.getServletContext().getRealPath("img/category"));
        File file = new File(imageFolder, id + "jpg");
        file.delete();
        return "redirect:/admin_category_list";
    }

    @RequestMapping("admin_category_edit")
    public String edit(Model model, int id){
        Category category = categoryService.get(id);
        model.addAttribute("category", category);
        return "admin/editCategory";
    }

    @RequestMapping("admin_category_update")
    public String update(HttpSession session, Category category, UploadedImageFile uploadedImageFile) throws IOException {
        categoryService.update(category);

        File imageFolder = new File(session.getServletContext().getRealPath("img/category"));
        File file = new File(imageFolder, category.getId() + ".jpg");   //第一个参数是目录，第二个参数是文件名
        //不存在文件夹就创建
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        uploadedImageFile.getImage().transferTo(file);
        BufferedImage bufferedImage = ImageUtil.change2jpg(file);
        ImageIO.write(bufferedImage, "jpg", file);
        return "redirect:/admin_category_list";
    }
}
